package com.bulletelc.apollopre.module.main.center.collection;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.CollectionBean;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.utils.ToastUtil;
import com.bulletelc.apollopre.widget.FoolBar;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CX on 2018/1/24.
 * 收藏界面
 */

public class CollectionActivity extends MVPBaseActivity<CollectionContract.View, CollectionPresenter> implements CollectionContract.View,
        CollectionsListAdapter.OnClickListener,FoolBar.ClickCallback {

    private FoolBar flBar;
    private ListView listView;
    private TextView tvNoCollections;
    private List<CollectionBean> collectionList;
    private CollectionsListAdapter adapter;


    @Override
    public int getLayoutRes() {
        return R.layout.collection_list_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        flBar.setTitle(this.getResources().getString(R.string.center_collect));
        listView = findView(R.id.lv_collection);
        tvNoCollections = findView(R.id.tv_no_collection);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        collectionList = new ArrayList<>();
        adapter = new CollectionsListAdapter(this, collectionList);
        adapter.setOnClickListener(this);
        listView.setAdapter(adapter);
        presenter.getCollectionsList();
    }

    @Override
    public void onSuccessCollectionsList(List<CollectionBean> collections) {
        if(collections != null && collections.size() > 0) {
            tvNoCollections.setVisibility(View.GONE);
            collectionList.clear();
            collectionList.addAll(collections);
            adapter.notifyDataSetChanged();
        } else {
            tvNoCollections.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }

    }

    @Override
    public void onFailCollectionsList(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorCollectionsList(String error) {
        ToastUtil.showLongToast(this,R.string.network_msg);
    }

    @Override
    public void onSuccessRemove(int position) {
        collectionList.remove(position);
        if(collectionList.size() > 0) {
            adapter.notifyDataSetChanged();
        } else {
            tvNoCollections.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onFailRemove(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorRemove(String error) {
        Toast.makeText(this, R.string.network_msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected CollectionPresenter createPresenter() {
        return new CollectionPresenter();
    }

    @Override
    public void onClick(int position) {
        presenter.removeItem(collectionList.get(position).getId(),position);
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }
}
